//author:kakake
class DeptManageController {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn()
    }

    public TableModel = new Model().Books;

    getdata() {
        var pageInfo = {
            key: "Id",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };


        this.TableModel.where = TSLobsterAPI.DbHelper.GetWhere(this.TableModel.query);
        this.TableModel.pageinfo = pageInfo;

        TSLobsterAPI.Utils.TrackLog("where", this.TableModel.where);
        TSLobsterAPI.Utils.TrackLog("WorkId", this.TableModel.WorkId);

        var datagrid = { data: {}, count: 0 };
        datagrid.data = TSLobsterAPI.DbHelper.GetListModelPage(this.data.dbConn, this.TableModel);
        datagrid.count = pageInfo.totalRecord;

        return datagrid;
    }

    savemodel() {
        let self = this;
        self.TableModel.objval = JSON.parse(TSLobsterAPI.Program.RequestBody());

        if (self.existbookname(self.TableModel.objval.BookName) && self.TableModel.objval.Id == 0) {
            throw '请不要添加重复的书籍名称';
        }

        TSLobsterAPI.Utils.TrackLog("TableModel", self.TableModel);
        TSLobsterAPI.DbHelper.SaveModel(self.data.dbConn, self.TableModel);
        return { result: true };
    }

    deletemodel() {
        let self = this;
        self.TableModel.val = TSLobsterAPI.Program.RequestQuery("Id", "0");
        TSLobsterAPI.DbHelper.DeleteModel(self.data.dbConn, self.TableModel);
        return { result: true };
    }

    existbookname(bookname: string) {
        let self = this;
        let strsql = "select count(1) ct from Books where bookname=?"
        var dbparams = [];
        dbparams.push(bookname);
        var res = TSLobsterAPI.DbHelper.DbGetJsonObject(self.data.dbConn, strsql, dbparams);
        if (res.ct > 0) {
            return true;
        } else {
            return false;
        }
    }
}